package com.itheima.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.BlogMapper;
import com.itheima.pojo.Blog;
import com.itheima.utils.DateUtils;
import com.itheima.utils.IdWorker;
import com.itheima.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @Version: V1.0
 */
@Service
public class BlogService {
    @Autowired
    private BlogMapper blogMapper;

    public void add(Blog blog) {
        Date time = DateUtils.strToDateTime(DateUtils.getNowTime());
        if(blog.getUid()==null){
            blog.setUid(String.valueOf(new IdWorker().nextId()));
        }
        if(blog.getTitle()==null){
            blog.setTitle("");
        }
        if(blog.getSummary()==null){
            blog.setSummary("");
        }
        if(blog.getContent()==null){
            blog.setContent("");
        }
        if(blog.getTagUid()==null){
            blog.setTagUid("");
        }
        if(blog.getClickCount()==null){
            blog.setClickCount(0);
        }
        if(blog.getCollectCount()==null){
            blog.setCollectCount(0);
        }
        if(blog.getFileUid()==null){
            blog.setFileUid("");
        }
        if(blog.getStatus()==null){
            blog.setStatus(1);
        }
        if(blog.getCreateTime()==null){
            blog.setCreateTime(time);
        }
        if(blog.getUpdateTime()==null){
            blog.setUpdateTime(time);
        }
        if(blog.getAdminUid()==null){
            blog.setAdminUid("");
        }
        if(blog.getIsOriginal()==null){
            blog.setIsOriginal("1");
        }
        if(blog.getAuthor()==null){
            blog.setAuthor("");
        }
        if(blog.getArticlesPart()==null){
            blog.setArticlesPart("");
        }
        if(blog.getBlogSortUid()==null){
            blog.setBlogSortUid("");
        }
        if(blog.getLevel()==null){
            blog.setLevel(0);
        }
        if(blog.getIsPublish()==null){
            blog.setIsPublish("1");
        }
        if(blog.getSort()==null){
            blog.setSort(0);
        }
        if(blog.getStartComment()==null){
            blog.setStartComment(1);
        }
        blogMapper.insertSelective(blog);

    }

    public void edit(Blog blog) {
        Date time = DateUtils.strToDateTime(DateUtils.getNowTime());
        blog.setUpdateTime(time);
        blogMapper.updateByPrimaryKeySelective(blog);
    }

    public void delete(Blog blog) {
        blogMapper.deleteByPrimaryKey(blog.getUid());
    }

    public void deleteBatch(List<Blog> list) {
        if(list!=null&&list.size()>0){
            for (Blog blog : list) {
                blogMapper.deleteByPrimaryKey(blog.getUid());
            }
        }
    }

    public PageResult<Blog> getList(Blog blog) {
        PageHelper.startPage(blog.getCurrentPage(), blog.getPageSize());
        Page<Blog> blogs = (Page<Blog>) blogMapper.selectAll();
        return new PageResult<Blog>(blogs.getTotal(), blogs.getResult());
    }
}
